package com.gx.smart.building;

import com.gx.smart.lib.http.api.asynctask.CallBack;
import com.gx.smart.lib.http.api.asynctask.GrpcAsyncTask;
import com.gx.smart.lib.http.api.config.ApiConfig;
import com.gx.smart.lib_http.BuildConfig;
import com.huishi.gx.ib.grpc.lib.ib.asset.asset.v1.AssetTypeApiGrpc;
import com.huishi.gx.ib.grpc.lib.ib.asset.asset.v1.AssetTypeListQueryReq;
import com.huishi.gx.ib.grpc.lib.ib.asset.asset.v1.AssetTypeListQueryResp;
import com.huishi.gx.ib.grpc.lib.ib.asset.asset.v1.AssetTypeQueryResp;
import com.huishi.gx.ib.grpc.lib.ib.asset.asset.v1.AssetTypeTreeQueryReq;
import com.orhanobut.logger.Logger;

import java.util.concurrent.TimeUnit;

import io.grpc.ManagedChannel;

/**
 * @author: xiaosy
 * 时间: 2022/4/12
 * @Description: java类作用描述
 * @Version: 1.0
 **/
public class AssetTypeApiService {
    private String host = BuildConfig.JOYLIFE_GRPC_UAA_SERIVER_URL;
    private String port = "80";

    private static class SingleTonHolder {
        private static final AssetTypeApiService INSTANCE = new AssetTypeApiService();
    }

    public static AssetTypeApiService getInstance() {
        return SingleTonHolder.INSTANCE;
    }

    private AssetTypeApiService() {

    }

    /**
     * 获取AuthStub  设置超时时间  8秒
     *
     * @param channel
     * @return
     */
    private AssetTypeApiGrpc.AssetTypeApiBlockingStub getStub(ManagedChannel channel) {
        return AssetTypeApiGrpc.newBlockingStub(channel)
                .withDeadlineAfter(ApiConfig.TIMEOUT_NETWORK, TimeUnit.SECONDS);

    }

    /**
     * 查询资产类型树
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, AssetTypeQueryResp> queryAssetTypeTree(
            CallBack<AssetTypeQueryResp> callBack) {
        return new GrpcAsyncTask<String, Void, AssetTypeQueryResp>(callBack) {
            @Override
            protected AssetTypeQueryResp doRequestData(ManagedChannel channel) {
                AssetTypeTreeQueryReq message = AssetTypeTreeQueryReq.newBuilder()
                        .build();
                Logger.d(message);
                AssetTypeQueryResp response = null;
                try {
                    response = getStub(channel).queryAssetTypeTree(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "queryAssetTypeTree");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

    /**
     * 通过父节点ID查询下级资产类型列表
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, AssetTypeListQueryResp> queryAssetTypeListByParentId(CallBack<AssetTypeListQueryResp> callBack) {
        return new GrpcAsyncTask<String, Void, AssetTypeListQueryResp>(callBack) {
            @Override
            protected AssetTypeListQueryResp doRequestData(ManagedChannel channel) {
                AssetTypeListQueryReq message = AssetTypeListQueryReq.newBuilder()
                        .build();
                Logger.d(message);
                AssetTypeListQueryResp response = null;
                try {
                    response = getStub(channel).queryAssetTypeListByParentId(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "queryAssetTypeListByParentId");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

}
